package com.focus.xyz.union.pages.frontend.forms;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseForm;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.data.InsuredBean;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/5/3.
 */
public class InsuredForm extends BaseForm {
    private Action action;
    private static final String NODE_XPATH = "tbody/tr/td/select|tbody/tr/td/input";
    private WebDriver driver;


    /*添加被保人按钮*/
    @FindBy(id = "dev_moreinsurant")
    private WebElement addInsuredBtn;
    /*被保人信息所在table*/
    @FindBy(xpath = "//table[@class='insured-table-cont dev_insurant_table_input']")
    private List<WebElement> insuredTableList;

    /*被保人的关系类型 1：本人 9：配偶 10：父母 11：子女 14：其他联系人 */
    private List<WebElement> insuredTypeRadios;

    /*被保人的关系类型 1：本人 9：配偶 10：父母 11：子女 14：其他联系人 */
    private List<WebElement> insuredGenderRadios;

    private String insuredNameId = "dev_name_";
    private String insuredEngNameId = "dev_engName_";
    private String insuredCardTypeId = "dev_cardType_";
    private String insuredJobTypeId1 = "insInsurantDtoList[%s].jobx_1";
    private String insuredJobTypeId2 = "insInsurantDtoList[%s].jobx_2";
    private String insuredJobTypeId = "insInsurantDtoList[%s].jobx";
    private String insuredCardNoId = "dev_cardNo_";
    private String insuredBirthDateId = "dev_birthday_";
    private String insuredMobileId = "dev_mobile_";
    //radios
    private String insuredTypeId = "dev_insurantType_";
    private String insuredGenderId = "dev_gender_";
    private int insuredIdx = 0;


    public InsuredForm(WebDriver driver, Action action){
        super(driver,action);
        this.action = action;
        this.driver = driver;
        PageFactory.initElements(this.driver,this);
    }
    @Override
    protected List<WebElement> getAllElements() throws Exception {
        List<WebElement> all = insuredTableList.get(0).findElements(By.xpath(NODE_XPATH));
        for(WebElement e:all){
            System.out.println("private String " +e.getAttribute(Constants.ATTR_ID)+";");
        }
        return all;
    }


    public void selectInsuredType(String target) throws Exception{
        insuredTypeRadios = driver.findElement(By.id(insuredTypeId + insuredIdx)).findElements(By.tagName(Constants.TAG_INPUT));
        if(this.insuredTypeRadios!=null&&this.insuredTypeRadios.size() > 0){
            if(this.insuredTypeRadios.size() == 5){
                if("本人".equals(target)){
                    action.click(this.insuredTypeRadios.get(0));
                }else if("配偶".equals(target)){
                    action.click(this.insuredTypeRadios.get(1));
                }else if("父母".equals(target)){
                    action.click(this.insuredTypeRadios.get(2));
                }else if("子女".equals(target)){
                    action.click(this.insuredTypeRadios.get(3));
                }else if("其他关系人".equals(target)){
                    action.click(this.insuredTypeRadios.get(4));
                }
            }else if(this.insuredTypeRadios.size() == 2){
                if("子女".equals(target)){
                    action.click(this.insuredTypeRadios.get(0));
                }else if("其他关系人".equals(target)){
                    action.click(this.insuredTypeRadios.get(1));
                }
            }else if(this.insuredTypeRadios.size() == 6){
                if("本人".equals(target)){
                    action.click(this.insuredTypeRadios.get(0));
                }else if("配偶".equals(target)){
                    action.click(this.insuredTypeRadios.get(1));
                }else if("父母".equals(target)){
                    action.click(this.insuredTypeRadios.get(2));
                }else if("子女".equals(target)){
                    action.click(this.insuredTypeRadios.get(3));
                }else if("有抚养或赡养关系的亲属".equals(target)){
                    action.click(this.insuredTypeRadios.get(4));
                }else if("其他关系人".equals(target)){
                    action.click(this.insuredTypeRadios.get(5));
                }
            }
            /*for(WebElement e:insuredTypeRadios){
                WebElement ee = e.findElement(By.xpath("following-sibling"));
                if(ee.getText().contains(target)){
                    action.click(e);
                }
            }*/
        }
    }

    public void selectInsuredGender(String target) throws Exception{
        insuredGenderRadios = driver.findElement(By.id(insuredGenderId + insuredIdx)).findElements(By.tagName(Constants.TAG_INPUT));
        if(this.insuredGenderRadios!=null&&this.insuredGenderRadios.size() > 0){
            if("男".equals(target)){
                action.click(this.insuredGenderRadios.get(0));
            }else if("女".equals(target)){
                action.click(this.insuredGenderRadios.get(1));
            }
        }
    }

    public int getInsuredTables() throws Exception{
        return insuredTableList.size();
    }

    public void inputInsuredName(String insuredName) throws Exception{
        action.inputText(driver.findElement(By.id(insuredNameId + insuredIdx)),insuredName);
    }

    public void inputInsuredEngName(String engName) throws Exception{
        if(action.isElementExsit(By.id(insuredEngNameId + insuredIdx))){
            action.inputText(driver.findElement(By.id(insuredEngNameId + insuredIdx)),engName);
        }
    }

    public void selectCardType(String type) throws Exception{
        action.select(driver.findElement(By.id(insuredCardTypeId + insuredIdx)),type);
    }

    public void selectInsuredJob(String jobx1,String jobx2,String jobx) throws Exception{
        if(action.isElementExsit(By.id(String.format(insuredJobTypeId1,insuredIdx)))){
            action.select(driver.findElement(By.id(String.format(insuredJobTypeId1,insuredIdx))),jobx1);
        }
        if(action.isElementExsit(By.id(String.format(insuredJobTypeId2,insuredIdx)))){
            action.select(driver.findElement(By.id(String.format(insuredJobTypeId2,insuredIdx))),jobx2);
        }
        if(action.isElementExsit(By.id(String.format(insuredJobTypeId,insuredIdx)))){
            action.select(driver.findElement(By.id(String.format(insuredJobTypeId,insuredIdx))),jobx);
        }
    }

    public void inputCardNo(String cardNo) throws Exception{
        WebElement el = driver.findElement(By.id(insuredCardNoId + insuredIdx));
        action.inputText(el,cardNo);
    }

    public void inputBirthDate(String birthdate) throws Exception{
        action.inputText(driver.findElement(By.id(insuredBirthDateId + insuredIdx)),birthdate);
        action.esc();
    }

    public void inputMobile(String mobile) throws Exception{
        action.inputText(driver.findElement(By.id(insuredMobileId + insuredIdx)),mobile);
    }

    //点击 “添加被保险人”按钮
    public void clickAddInsuredBtn() throws Exception{
        action.click(addInsuredBtn);
        insuredIdx++;
    }

    /*填写被保人表单*/
    public void fillInsured(InsuredBean bean) throws Exception{
        String jobs = bean.getDev_jobs();
        if(StringUtils.isNotBlank(jobs)){
            String[] jj = jobs.split("-");
            if(jj.length == 3){
                selectInsuredJob(jj[0].trim(),jj[1].trim(),jj[2].trim());
            }
        }
        selectInsuredType(bean.getDev_insurantType());
        inputInsuredName(bean.getDev_name());
        inputInsuredEngName(bean.getDev_engName());
        String cardType = bean.getDev_cardType();
        selectCardType(bean.getDev_cardType());
        inputCardNo(bean.getDev_cardNo());
        if(!"身份证".equals(cardType.trim())){
            inputBirthDate(bean.getDev_birthday());
            selectInsuredGender(bean.getDev_gender());
        }
        inputMobile(bean.getDev_mobile());
    }


    /*添加被保险人,根据excel中的被保险人的数量添加*/
    public void addInsured(String insuredId) throws Exception{
        if(StringUtils.isNotBlank(insuredId)){
            String[] ids = insuredId.split(",");
            List<InsuredBean> list = InsDataService.getInsuredById(ids);
            for (int i = 0; i<list.size();i++){
                fillInsured(list.get(i));
                if(i == list.size() -1){
                    break;
                }
                clickAddInsuredBtn();
            }
        }
    }





}
